import numpy as np
import pygame
from pieces import Pieces

class Board():
    def __init__(self):
        self.place = np.zeros((15, 15)) #棋盘
        # self.movableSite = []
        self.movableNum = 0
        self.img = pygame.image.load('img/board.png')
        self.img = pygame.transform.scale(self.img,(600,600))
        for i in range(15):
            for j in range(15):
                self.movableNum += 1
                # self.movableSite.append((i, j))

    def getSite(self, site):
        return self.place[site]

    def allMovable(self):
        return (self.movableNum, self.movableNum)

    def move(self, pieces):
        self.place[pieces.site] = pieces.id #更新棋盘状态
        self.movableNum -= 1 #剩余空位减少
        # self.movableSite.remove(pieces.site)

    def __judge(self, x, y, id, label, res):
        if x < 0 or x > 14 or y < 0 or y > 14 or self.place[x, y] != id:
            return res
        if label == 0: #判断'——'负方向的连子情况
            return self.__judge(x - 1, y, id, label, res + 1)
        elif label == 1: #判断'/'负方向的连子情况
            return self.__judge(x - 1, y + 1, id, label, res + 1)
        elif label == 2: #判断'1'负方向的连子情况
            return self.__judge(x, y + 1, id, label, res + 1)
        elif label == 3: #判断'\'负方向的连子情况
            return self.__judge(x + 1, y + 1, id, label, res + 1)
        elif label == 4: #判断'——'正方向的连子情况
            return self.__judge(x + 1, y, id, label, res + 1)
        elif label == 5: #判断'/'正方向的连子情况
            return self.__judge(x + 1, y - 1, id, label, res + 1)
        elif label == 6: #判断'1'正方向的连子情况
            return self.__judge(x, y - 1, id, label, res + 1)
        elif label == 7: #判断'\'正方向的连子情况
            return self.__judge(x - 1, y - 1, id, label, res + 1)

    #判断一个棋子
    def judge(self, pieces):
        x = pieces.site[0]
        y = pieces.site[1]
        id = pieces.id
        #判断四个方向的连子情况
        sum1 = self.__judge(x - 1, y, id, 0, 0) + self.__judge(x + 1, y, id, 4, 0)
        sum2 = self.__judge(x - 1, y + 1, id, 1, 0) + self.__judge(x + 1, y - 1, id, 5, 0)
        sum3 = self.__judge(x, y + 1, id, 2, 0) + self.__judge(x, y - 1, id, 6, 0)
        sum4 = self.__judge(x + 1, y + 1, id, 3, 0) + self.__judge(x - 1, y - 1, id, 7, 0)
        # 除去当前棋子，四个方向最大连子数大于等于4，则五连子胜利
        if max(sum1,sum2,sum3,sum4) >= 4:
            return (True,id)
        else:
            return (False,id)

    def update(self,screen:pygame.Surface):
        screen.blit(self.img,(0,0))

    def getScore(self,x,y,is_first):
        score=0
        #对不同布局的评分
        val=[[0,0,0,0,1361],
             [1,5,21,170,1361],
             [5,21,85,340,1361]]
        #四个方向
        x_dir=[-1,0,1,1]
        y_dir=[1,1,1,0]
        for direction in range(0,4):
            emptyNum=0
            lianNum=1
            for is_postiveDir in range(-1,2,2):
                x_new = x + is_postiveDir * x_dir[direction]
                y_new = y + is_postiveDir * y_dir[direction]
                #当在棋盘范围内且遍历的点不为人类的棋子时继续循环
                while(x_new>=0 and x_new<15 and y_new>=0 and y_new<15 and self.place[x_new,y_new] != (1 if is_first == 1 else 2)):
                    if self.place[x_new,y_new] == 0:
                        emptyNum+=1
                        break
                    else:
                        lianNum+=1
                    x_new += is_postiveDir * x_dir[direction]
                    y_new += is_postiveDir * y_dir[direction]
            if lianNum > 5:
                lianNum=5
            score+=val[emptyNum][lianNum-1]
        return score